package com.atguigu.tingshu.album.service.impl;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.io.FileUtil;
import com.atguigu.tingshu.album.config.MinioConstantProperties;
import com.atguigu.tingshu.album.service.AuditService;
import com.atguigu.tingshu.album.service.FileUploadService;
import com.atguigu.tingshu.common.execption.GuiguException;
import com.atguigu.tingshu.common.result.ResultCodeEnum;
import io.minio.MinioClient;
import io.minio.PutObjectArgs;
import io.minio.errors.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.util.UUID;
import java.util.logging.LogRecord;

import static com.atguigu.tingshu.common.constant.SystemConstant.*;

/**
 * @author: atguigu
 * @create: 2025-05-28 09:46
 */
@Slf4j
@Service
public class FileUploadServiceImpl implements FileUploadService {

    @Autowired
    private MinioClient minioClient;

    @Autowired
    private MinioConstantProperties minioConstantProperties;

    @Autowired
    private AuditService auditService;

    /**
     * 图片上传到MInIO文件服务器
     *
     * @param file
     * @return
     */
    @Override
    public String fileUpload(MultipartFile file) {
        try {
            //1.校验文件是否为图片
            BufferedImage bufferedImage = ImageIO.read(file.getInputStream());
            if (bufferedImage == null) {
                throw new GuiguException(500, "非法图片");
            }
            //2.限制图片文件大小 要求：900*900 像素，大小不超过2M
            int height = bufferedImage.getHeight();
            int width = bufferedImage.getWidth();
            if (height > 900 || width > 900) {
                throw new GuiguException(500, "图片限制900*900");
            }

            //对图片内容进行校验是否合法
            String suggestion = auditService.auditImage(file);
            if ("review".equals(suggestion) || "block".equals(suggestion)) {
                throw new GuiguException(500, "图片内容不合法");
            }
            //3.调用MInIOCLient对象文件上传
            //3.1 生成上传到MINIO文件唯一标识作为文件名称
            String folderName = "/" + DateUtil.today();
            String extName = FileUtil.extName(file.getOriginalFilename());
            String objectName = folderName + "/" + UUID.randomUUID().toString() + "." + extName;

            //3.2 调用putObject方法上传文件
            minioClient.putObject(
                    PutObjectArgs.builder().bucket(minioConstantProperties.getBucketName()).object(objectName).stream(
                                    file.getInputStream(), file.getSize(), -1)
                            .contentType("video/mp4")
                            .build());
            //4.返回文件访问路径 http://192.168.200.6:9000/tingshu/2023-05-05/f0d0f7c5-c0c5-4c0.png
            return minioConstantProperties.getEndpointUrl() + "/" + minioConstantProperties.getBucketName() + objectName;
        } catch (Exception e) {
            log.error("文件上传失败", e);
            throw new GuiguException(500, e.getMessage());
        }

    }
}
